package com.tcm.pay.web.controller;

import com.tcm.pay.service.OrderNotifyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

/**
 * 聚合支付回调
 * 链接参数形式
 * http://api.zetikeji.com/api/pay/consultation/ali/notify
 * ?buyUserAccount=2088102114385380&FundChannel=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22ALIPAYACCOUNT%22%7D%5D&Amount=0.01&SignInfo=ptKSRUHBVl0J4Wrw5suzDB3eZTJ04SyUlDXA1FCj0JWaRRqlPKJ/V7h/QcIRATOyXip4vENaw2nRsvopwKPWdMr/P3rNVI2CwK1qBvYgITdlQbgyKI/8KysWGUkwI5nzAa/kg1qV1iiJSLnAZj8PtxVcOTzJlH4wQAXQOnlAzo8=&OrderNo=2522024704&PayType=AliJsapiPay_OffLine&Succeed=88&Result=SUCCESS&BillNo=2012021071006171004&FinishTime=20210706100631&ChannelNo=112021070622001485381432048898&MerNo=50829&BankNo=2522024704
 */
@RequestMapping("/pay")
@RestController
public class ConsultationNotifyController {

    @DubboReference(version = "0.0.1-alpha", group = "consultation",check = false, timeout = 3000)
    private OrderNotifyService orderNotifyService;


    /**
     * 微信支付通知
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/consultation/wx/notify")
    public String orderWxpay(@RequestParam(name = "Succeed", value = "") String succeed,
                         @RequestParam(name = "BillNo", value = "") String billNo,
                         @RequestParam(name = "OrderNo", value = "") String orderNo,
                         @RequestParam(name = "ChannelNo", value = "") String payChannelNo) throws Exception {
        String content = "成功:"+succeed + "\r\n编号：" + billNo + "\r\n订单号：" +
                 orderNo + "\r\n支付渠道号：" + payChannelNo;

        return orderNotifyService.wxpay(succeed,billNo,orderNo,payChannelNo);
    }

    /**
     * 支付宝支付通知
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/consultation/ali/notify")
    public String orderAlipay(@RequestParam(name = "Succeed", value = "") String succeed,
                             @RequestParam(name = "BillNo", value = "") String billNo,
                             @RequestParam(name = "OrderNo", value = "") String orderNo,
                             @RequestParam(name = "ChannelNo", value = "") String payChannelNo) throws Exception {
        String content = "成功:"+succeed + "\r\n编号：" + billNo + "\r\n订单号：" +
                orderNo + "\r\n支付渠道号：" + payChannelNo;
       // writeListToFile(content);
        return orderNotifyService.alipay(succeed,billNo,orderNo,payChannelNo);
    }

    /**
     * 实现写操作方法
     */
    private void writeListToFile(String content) {
        File file = new File("/home/zetikeji/log/pay.log");// 要写入的文件路径
        if (!file.exists()) {// 判断文件是否存在
            try {
                file.createNewFile();// 如果文件不存在创建文件
                System.out.println("文件"+file.getName()+"不存在已为您创建!");
            } catch (IOException e) {
                System.out.println("创建文件异常!");
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        PrintStream ps = null;
        try {
            fos = new FileOutputStream(file,false);// 文件输出流	追加
            ps = new PrintStream(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ps.print(content); // 执行写操作
        ps.close();	// 关闭流

        System.out.println("文件写入完毕!");
    }
}
